package com.yellowpage.omc.ability.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;

import com.yellowpage.omc.ability.dao.AppInterfaceDao;
import com.yellowpage.omc.ability.data.AppInterface;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class AppInterfaceServiceImpl extends BaseServiceImpl implements
		AppInterfaceService {
	private AppInterfaceDao appInterfaceDao;

	public void setAppInterfaceDao(AppInterfaceDao appInterfaceDao) {
		this.appInterfaceDao = appInterfaceDao;
	}

	/* Smooth generate Method */
	public AppInterface getAppInterface(Long id) {
		return appInterfaceDao.get(id);
	}

	public List<AppInterface> getAllAppInterface() {
		return appInterfaceDao.getAll();
	}

	public List<Map> queryAppInterfaceByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"SELECT a.INTERFACE_ID , a.INTERFACE_CODE , a.URL , a.NOTE ," +
						"       (select display_name from CA_USER where user_id = a.LAST_UPDATE_USER) as last_update_user, " +
						"       (select display_name from CA_USER where user_id = a.CREATE_USER) as create_user, " +
						"       a.CREATE_TIME , a.LAST_UPDATE_TIME ," +
						" a.NAME , a.TYPE , a.STATUS , (SELECT display_name FROM CA_USER WHERE user_id = a.LAST_SYNCHRONIZATION_USER) AS last_synchronization_user,a.LAST_SYNCHRONIZATION_TIME FROM OMC_INTERFACE a WHERE 1=1 ")
				.addCondition(" and INTERFACE_ID = :interfaceId ")
				.addCondition(" and INTERFACE_CODE like :interfaceCode ")
				.addCondition(" and NAME like :name ")
				.addCondition(" and URL = :url ")
				.addCondition(" and NOTE = :note ")
				.addCondition(" and CREATE_USER = :createUser ")
				.addCondition(
						" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
				.addCondition(" and LAST_UPDATE_USER = :lastUpdateUser ")
				.addCondition(
						" and date_format(LAST_UPDATE_TIME,'%Y-%m-%d') =  :lastUpdateTime  ")
				.addCondition(" and TYPE = :type ")
				.addCondition(" and STATUS = :status ")
				.addCondition(" and LAST_SYNCHRONIZATION_USER = :LastSynchronizationUser ")
				.addCondition(" and LAST_SYNCHRONIZATION_TIME = :LastSynchronizationTime ");
		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
	
	
	public Map getAppInterfaceMap(){
    	Map dataSourceMap = new LinkedMap();
    	
    	QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
		" SELECT   a.NAME as NAME , a.INTERFACE_ID as VALUE   FROM OMC_INTERFACE a WHERE  a.STATUS = '1'  ")
					    ; 	
         
		List<Map> resultList = sqlAssistant.query(
				baseDao.getConnection(), new HashMap());
				
		
		for(int i = 0; i < resultList.size(); i++){
			dataSourceMap.put(resultList.get(i).get("VALUE"), resultList.get(i).get("NAME"));
		}
		
		return dataSourceMap;
    }
	

	public void saveAppInterface(AppInterface appInterface) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("select sn.seq_num as seq_num from OMC_SEQ_NUM sn where sn.seq_name = 'OMC_INTERFACE_CODE' ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		Integer codeNum = (Integer) resultList.get(0).get("SEQ_NUM");
		
		try{
    		
		    baseDao.getConnection().createStatement().execute("update  OMC_SEQ_NUM sn " +
		    		"   set sn.seq_num = " + (codeNum.intValue() + 1) + 
		    		"  where sn.seq_name = 'OMC_INTERFACE_CODE'");
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		String code = "";

		for (int i = 0; i < (6 - codeNum.toString().length()); i++) {
			code = code + "0";
		}
		code = code + codeNum.toString();
		
		appInterface.setInterfaceCode(code);

		appInterfaceDao.save(appInterface);
	}

	public void updateAppInterface(AppInterface appInterface) {
		appInterfaceDao.update(appInterface);
	}

	public void delete(List deleteList) {
		appInterfaceDao.remove(deleteList);
	}

}